

/***
 * 转换数据类型
 */
function convertData (type, value) {
  type = typeof type == 'undefined' || type == null || type == '' ? 'string' : type;
  if (typeof value == "undefined" || value == null || value == '') {
    if (type == 'object') {
      return {};
    } else if (type == 'array') {
      return [];
    } else if (type == 'int') {
      // return parseInt()
      return 0;
    } else if (type == 'float') {
      return 0.0;
    } else {
      return '';
    }
  }
  if (type == 'object') {
    return JSON.parse(value);
  } else if (type == 'array') {
    return JSON.parse(value);;
  } else if (type == 'int') {
    return parseInt(value);
  } else if (type == 'float') {
    return parseFloat(value);
  } else {
    return value;
  }
}

//缓存操作
let storage = {
  get: function (key, type) {
    let value = localStorage.getItem(key);
    return convertData(type, value);
  },
  set: function (key, value) {
    if (typeof value == 'object') {
      value = JSON.stringify(value);
    } else if (typeof value == 'number') {
      value = '' + value;
    }
    localStorage.setItem(key, value);
  },
  remove: function (key) {
    localStorage.removeItem(key);
  }
};

//本地session数据操作
let session = {
  set: function (key, value) {
    if (typeof value == 'object') {
      value = JSON.stringify(value);
    } else if (typeof value == 'number') {
      value = '' + value;
    }
    sessionStorage.setItem(key, value);
  },
  get: function (key, type) {
    let value = sessionStorage.getItem(key);
    return convertData(type, value);
  },
  remove: function (key) {
    sessionStorage.removeItem(key);
  },
  clear: function () {
    sessionStorage.clear();
  }
};


//token操作
let token = {
  /***
   * 获取本地token
   */
  get: function () {
    let token = session.get('token');
    if (!token) {
      return '';
    }
    return token;
  },

  /***
   * 删除本地token
   */
  remove: function () {
    try {
      session.remove("token");
    } catch (e) {

    }
  },

  /***
   * 设置token
   */
  set: function (token) {
    session.set('token', token);
  }

};

/**
 * 获取GET请求参数
 * @param {string} params 
 */
function GetRequest (params) {
  //获取url中"?"符后的字串
  let url = decodeURIComponent(location.href);
  let index = url.indexOf("?");
  if (index == -1) {
    return false;
  }
  url = url.substring(url.indexOf("?"));
  let theRequest = new Object();
  let str = url.substr(1);
  let strs = str.split("&");
  for (var i = 0; i < strs.length; i++) {
    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  }
  if (typeof params != "undefined") {
    if (typeof theRequest[params] != "undefined") {
      return theRequest[params];
    }
    return false;
  }
  return theRequest;
}

var encrypt = {
  // 加密
  encode (str) {
    var r = "";
    for (var i = 0; i < str.length; i++) {
      var code = str.charCodeAt(i);
      r += code;
      r += ",";
    }

    return r.replace(/(^\s*)|(\s*$)/g, "");
  },

  decode (str) {
    var arr = str.split(",");
    var r = "";
    for (var i = 0; i < arr.length; i++) {
      var code = parseInt(arr[i]);
      r += String.fromCharCode(code);
    }
    return r.replace(/(^\s*)|(\s*$)/g, "");
  }

}




export default {
  convertData,
  storage,
  session,
  token,
  GetRequest,
  encrypt
}